package com.fzdy.dataplatform.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fzdy.dataplatform.controller.api.DelArr;
import com.fzdy.dataplatform.controller.api.ModuleCallback;
import com.fzdy.dataplatform.controller.api.announcement.AnnouncementListApi;
import com.fzdy.dataplatform.controller.api.sysLog.LogStringUtil;
import com.fzdy.dataplatform.controller.api.sysLog.SysLogApi;
import com.fzdy.dataplatform.controller.api.user.UserListApi;
import com.fzdy.dataplatform.core.common.AjaxCallback;
import com.fzdy.dataplatform.core.common.TimeStampTool;
import com.fzdy.dataplatform.core.controller.BaseController;
import com.fzdy.dataplatform.core.vo.Page;
import com.fzdy.dataplatform.entity.Announcement;
import com.fzdy.dataplatform.entity.User;
import com.fzdy.dataplatform.entity.UserAnnouncement;
import com.fzdy.dataplatform.service.AnnouncementService;
import com.fzdy.dataplatform.service.UserAnnouncementService;
import com.fzdy.dataplatform.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/sysmanage/announcement")
public class AnnouncementController extends BaseController {
    @Autowired
    private AnnouncementService announcementService;
    @Autowired
    private UserAnnouncementService userAnnouncementService;
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/list")
    public String list(@RequestBody Page<Announcement> page) {
        announcementService.getPageByExampleEq(new Announcement(),page);
        AjaxCallback ajaxCallback = new AjaxCallback(dbtolist(page.getList()), page.getTotalRow());
        return JSON.toJSONString(ajaxCallback);
    }

    private List dbtolist(List<Announcement> list) {
        List<AnnouncementListApi> apis=new ArrayList<>();
        for (Announcement announcement:list) {
            apis.add(new AnnouncementListApi(announcement.getId(),announcement.getTitle(),announcement.getContent(),
                    TimeStampTool.DateToString("yyyy-MM-dd HH:mm:ss",announcement.getCreatedate()),
                    announcement.getUser().getName(),null,announcement.getId(),announcement.getSendto()));

        }
        return apis;
    }

    @RequestMapping(value = "/allUser")
    public String allUser() {
        User user=new User();
        user.setStatus(1);
        user.setRoid(2);
        List<User> list = userService.findByExampleEq(user);
        List<UserListApi> apis=new ArrayList<>();
        for (User u :list) {
            apis.add(new UserListApi(u.getId(),u.getName()));
        }
        AjaxCallback ajaxCallback = new AjaxCallback(apis, null);
        return JSON.toJSONString(ajaxCallback);
    }

    @RequestMapping(value = "/read")
    public String read(@RequestBody Announcement announcement, HttpServletRequest request) {
        User user=webUtilify.LoginUserinfo(request);
        UserAnnouncement userAnnouncement=new UserAnnouncement(user.getId(),announcement.getId(),1);
        List<UserAnnouncement> list=userAnnouncementService.findByExampleEq(userAnnouncement);
        if (list.size()==0){
            //参数错误
        }else {
            userAnnouncement=list.get(0);
            userAnnouncement.setStatus(0);
            userAnnouncementService.updateT(userAnnouncement);
        }
        AjaxCallback ajaxCallback = new AjaxCallback();
        return JSON.toJSONString(ajaxCallback);
    }

    @RequestMapping(value = "/edit")
    public String edit(@RequestBody AnnouncementListApi api, HttpServletRequest request) {
        User user = webUtilify.LoginUserinfo(request);
        Announcement announcement=new Announcement(api.getTitle(),api.getContent(),new Date(),user.getId());
        userAnnouncementService.insertT(api.getSendUsers(),announcement);
        //日志
        SysLogApi log_sysLogApi = new SysLogApi(LogStringUtil.ANNOUNCEMENT, LogStringUtil.SUCCESS, user.getId());
        JSONObject log_content = new JSONObject();
        log_content.put("公告信息",JSON.toJSONString(announcement));
        log_sysLogApi.setContent(log_content);
        syslogService.WriteLog(log_sysLogApi);

        AjaxCallback ajaxCallback = new AjaxCallback();
        return JSON.toJSONString(ajaxCallback);
    }

    @RequestMapping(value = "/mylist")
    public String mylist( HttpServletRequest request) {
        List<UserAnnouncement> list=userAnnouncementService.findByExampleEq(new UserAnnouncement(
                webUtilify.LoginUserinfo(request).getId(), null,1));
        AjaxCallback ajaxCallback = new AjaxCallback(dbToList(list),null);
        return JSON.toJSONString(ajaxCallback);
    }

    @RequestMapping(value = "/myAllList")
    public String myAllList(@RequestBody Page<UserAnnouncement> page, HttpServletRequest request) {
        userAnnouncementService.getPageByExampleEq(new UserAnnouncement(
                webUtilify.LoginUserinfo(request).getId(), null,null),page);
        AjaxCallback ajaxCallback = new AjaxCallback(dbToList(page.getList()),page.getTotalRow());
        return JSON.toJSONString(ajaxCallback);
    }

    private List<AnnouncementListApi> dbToList(List<UserAnnouncement> list) {
        List<AnnouncementListApi> apis=new ArrayList<>();
        for (com.fzdy.dataplatform.entity.UserAnnouncement UserAnnouncement:list) {
            Announcement announcement=UserAnnouncement.getAnnouncement();
            apis.add(new AnnouncementListApi(UserAnnouncement.getId(),announcement.getTitle(),announcement.getContent(),
                    TimeStampTool.DateToString("yyyy-MM-dd HH:mm:ss",announcement.getCreatedate()),
                    announcement.getUser().getName(),UserAnnouncement.getStatus(),UserAnnouncement.getAnnouncementid(),null));
        }
        return apis;

    }

    @RequestMapping(value = "/del")
    public String del(@RequestBody DelArr arr, HttpServletRequest request) {
        JSONObject log_content = new JSONObject();
        announcementService.batchDelete(arr.getIds());
        userAnnouncementService.deleteByIdT(0);
        log_content.put("id", arr.getIds());
        syslogService.WriteLog(new SysLogApi(LogStringUtil.DEL, LogStringUtil.ANNOUNCEMENT, LogStringUtil.SUCCESS,
                log_content, webUtilify.LoginUserinfo(request).getId()));
        //id验证
        AjaxCallback ajaxCallback = new AjaxCallback(ModuleCallback.SUCCESSNO, "请求成功");
        return JSON.toJSONString(ajaxCallback);
    }


}
